package com.iflytek.education.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.iflytek.common.constant.HttpStatus;
import com.iflytek.common.core.domain.TreeEntity;
import com.iflytek.common.exception.ServiceException;
import com.iflytek.education.domain.EduSubject;
import com.iflytek.education.domain.vo.OneSubject;
import com.iflytek.education.domain.vo.TwoSubject;
import com.iflytek.education.mapper.EduSubjectMapper;
import com.iflytek.education.service.IEduSubjectService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 课程类别Service业务层处理
 *
 * @author isahino
 * @date 2023-09-14
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements IEduSubjectService {

    @Resource
    private EduSubjectMapper eduSubjectMapper;

    private static final String TOP_ID = "0";

    /**
     * 查询课程类别
     *
     * @param id 课程类别主键
     * @return 课程类别
     */
    @Override
    public EduSubject selectEduSubjectById(String id) {
        return eduSubjectMapper.selectEduSubjectById(id);
    }

    /**
     * 查询课程类别列表
     *
     * @param eduSubject 课程类别
     * @return 课程类别
     */
    @Override
    public List<EduSubject> selectEduSubjectList(EduSubject eduSubject) {
        return eduSubjectMapper.selectEduSubjectList(eduSubject);
    }

    /**
     * 新增课程类别
     *
     * @param eduSubject 课程类别
     * @return 结果
     */
    @Override
    public int insertEduSubject(EduSubject eduSubject) {
        // 先查询是否有同样类别，有则抛出异常，不添加
        LambdaQueryWrapper<EduSubject> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EduSubject::getTitle, eduSubject.getTitle());
        if (count(queryWrapper) > 0) {
            throw new ServiceException("已存在此课程类别，不可重复添加");
        }
        return eduSubjectMapper.insert(eduSubject);
    }

    /**
     * 修改课程类别
     *
     * @param eduSubject 课程类别
     * @return 结果
     */
    @Override
    public int updateEduSubject(EduSubject eduSubject) {
        return eduSubjectMapper.updateById(eduSubject);
    }

    /**
     * 批量删除课程类别
     *
     * @param ids 需要删除的课程类别主键
     * @return 结果
     */
    @Override
    public int deleteEduSubjectByIds(String[] ids) {
        return eduSubjectMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除课程类别信息
     *
     * @param id 课程类别主键
     * @return 结果
     */
    @Override
    public int deleteEduSubjectById(String id) {
        // 先确保要删除的类别下没有子类别
        if (eduSubjectMapper.queryChildrenNum(id) > 0) {
            throw new ServiceException("课程类别下存在子类别，不可删除", HttpStatus.CONFLICT);
        }
        return eduSubjectMapper.deleteEduSubjectById(id);
    }

    @Override
    public List<OneSubject> getList() {
        //1.查询所有一级分类  parent_id=0
        LambdaQueryWrapper<EduSubject> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TreeEntity::getParentId, TOP_ID);
        List<EduSubject> oneSubjectList = this.baseMapper.selectList(wrapper);
        //2.查询所有二级分类 parent_id!=0
        wrapper = new LambdaQueryWrapper<>();
        wrapper.ne(TreeEntity::getParentId, TOP_ID);
        List<EduSubject> twoSubjectList = this.baseMapper.selectList(wrapper);
        //定义最终的返回类型
        List<OneSubject> finalSubjectList = new ArrayList<>(oneSubjectList.size());
        ///3 封装一级分类
        for (EduSubject subject : oneSubjectList) {
            //将oneSubjectList封装到finalSubjectList
            OneSubject oneSubject = new OneSubject();
            //以下两种方法都可
            BeanUtils.copyProperties(subject, oneSubject);
            //在一级分类循环遍历查询所有的二级分类, 创建list集合封装每一个一级分类的二级分类
            List<TwoSubject> children = new ArrayList<>();
            for (EduSubject eduSubject : twoSubjectList) {
                TwoSubject twoSubject = new TwoSubject();
                if (eduSubject.getParentId().equals(subject.getId())) {
                    BeanUtils.copyProperties(eduSubject, twoSubject);
                    children.add(twoSubject);
                }
            }
            oneSubject.setChildren(children);
            //加入结果集
            finalSubjectList.add(oneSubject);
        }
        return finalSubjectList;
    }
}
